 <template>
  <div class="box-catain">
   <el-form :model="ruleForm" :rules="rules" ref="ruleForm" >
   <el-form-item  prop="mobile">
    <el-input v-model="ruleForm.mobile" placeholder="请输入账号"></el-input>
  </el-form-item>
  <el-form-item prop="password">
    <el-input v-model="ruleForm.password" type='password' placeholder="请输入密码"></el-input>
  </el-form-item>
 
  <el-form-item>
  <el-button type='primary' @click="login">登录</el-button>
    <el-link type="info" @click="$router.push('/reg')">去注册</el-link>
  </el-form-item>
  
   </el-form>
  </div>
</template>

<script>
import {Login} from '@/api/http' 
import {mapState,mapMutations} from 'vuex'
export default {
data() {
    const samePwdFn = (rule,value,callback) => {
         if(value !== this.ruleForm.password){
            callback(new Error('两次输入的密码不一致！'))
         }else{
            callback()
         }
    }
    return {
        ruleForm:{
            mobile:'13800000002',
            password:'123456',

        },
        rules: {
          mobile: [
            { required: true, message: '请输入账号', trigger: 'blur' },
            { min: 3, max: 11, message: '长度在 3 到 11 个字符', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '请输入账号', trigger: 'blur' },
           
           {
            pattern:/^\S{6,15}$/,
            message: '密码必须是6-15的非空字符串',
             trigger: 'blur'


           }
          ],
          repassword: [
            { required: true, message: '请输入账号', trigger: 'blur' },
            {
            pattern:/^\S{6,15}$/,
            message: '密码必须是6-15的非空字符串',
             trigger: 'blur'
           },
           {validator: samePwdFn,  trigger: 'blur'},
          ],
        },
    }

},
methods: {
    login(){
          this.$refs.ruleForm.validate(async(valid) => {
          if (valid) {
           const res = await Login(this.ruleForm)
           console.log(res)
           console.log(res.data.code)

           if(res.status===200) { this.$message({
          message: '登录成功',
          type: 'success'
        });
        console.log(res.data.token)
        this.$store.commit('updataToken',res.data.data)
        // localStorage.setItem('token',res.data.data)
        this.$router.push({name:'layout'})
        // this.$store.token=res.data.token
        console.log(this.$store.state.token)
        }else {this.$message.error('错了哦，登录失败');}

          } else {
            console.log('error submit!!');
            return false;
          }
        });
    },

},
}
</script>

<style lang="less" scoped>
.box-catain{
    height: 100%;
    background-color: #333;
}
.el-form{
    box-shadow: rgba(1, 1, 1, .5);
    background-color: #fff;
    width: 300px;
    height: 300px;
    box-sizing: border-box;
    border: 1px #333 solid;
    position: absolute;
    top:50%;
    left: 50%;
    transform: translate(-50%,-50%);
    padding: 30px;
    .el-button{
    width: 100%;
    }
}
</style>